package ph.edu.upd.eee.upcatarchivesearch;

import android.graphics.Path;
import android.util.Log;

/**
 * Created by smartwire on 10/14/2014.
 */
public class HexTiles {
    private static int hexRadius;
    private static int hexWidth;
    private static int hexHeight;
    private static int hexTwoSide;

    private static int xRelCoords[];
    private static int yRelCoords[];

    public HexTiles(int radius) {
        // Compute variables
        this.hexRadius = radius;
        this.hexWidth = 2 * radius;
        this.hexHeight = (int) (Math.sqrt(3) * (double) radius);
        this.hexTwoSide = radius * 3 / 2;

        int xCoords[] = {radius / 2, hexTwoSide, hexWidth, hexTwoSide, radius / 2, 0};
        int yCoords[] = {0, 0, hexHeight / 2, hexHeight, hexHeight, hexHeight / 2};

        this.xRelCoords = xCoords;
        this.yRelCoords = yCoords;
    }

    public Path createHexPath(float leftX, float topY) {
        Path path;

        path = new Path();

        path.moveTo(xRelCoords[0] + leftX, yRelCoords[0] + topY);

        for(int i = 0; i < 6; i++) {
            path.lineTo(xRelCoords[(i + 1) % 6] + leftX, yRelCoords[(i + 1) % 6] + topY);
        }

        path.close();
        path.setFillType(Path.FillType.EVEN_ODD);

        return path;
    }

    public Path[] createTiledHexs(int wWidth, int wHeight) {
        int nHexRow, nHexCol, alternateAdd;
        Path[] paths;

        nHexRow = wWidth / hexTwoSide + 2;
        nHexCol = wHeight / hexHeight + 2;

        paths = new Path[nHexRow * nHexCol];

        // Draw by row, then by col
        for(int j = 0; j < nHexCol; j++) {
            for(int i = 0; i < nHexRow; i++) {
                alternateAdd = (i % 2 == 0) ? 0 : hexHeight / 2;
                paths[i + j * nHexRow] = createHexPath(hexTwoSide * (i - 1), j * hexHeight - alternateAdd);
            }
        }

        return paths;
    }
}
